package dwl.graduation.design.controller;

import dwl.graduation.design.domain.*;
import dwl.graduation.design.service.UserService;
import dwl.graduation.design.utils.MessageConstant;
import dwl.graduation.design.utils.PageResult;
import dwl.graduation.design.utils.QueryPageBean;
import dwl.graduation.design.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@Slf4j
@RequestMapping(path = "/user")
public class UserController {

    private final UserService userService;

    @Autowired
    public UserController(UserService userService) {
        this.userService = userService;
    }

    @RequestMapping(value = "/findUserList", method = RequestMethod.POST)
    public PageResult findUserList(@RequestBody QueryPageBean queryPageBean) {
        return userService.findUserList(queryPageBean);
    }

    @RequestMapping(value = "/insert", method = RequestMethod.POST)
    public Result insertUserInfo(@RequestBody UserInfoInsertRequest request) {
        try {
            boolean flag = userService.insertUserInfo(request);
            if (flag) {
                return new Result(true, MessageConstant.ADD_PERSON_SUCCESS);
            } else {
                return new Result(false, MessageConstant.ADD_PERSON_FAIL);
            }
        } catch (Exception e) {
            return new Result(false, MessageConstant.ADD_PERSON_FAIL);
        }
    }

    @RequestMapping(value = "/delete", method = RequestMethod.GET)
    public Result deleteUserInfo(@RequestParam("id") Integer id) {
        try {
            userService.deleteUserInfo(id);
            return new Result(true, MessageConstant.DELETE_PERSON_SUCCESS);
        } catch (Exception e) {
            return new Result(false, MessageConstant.DELETE_PERSON_FAIL);
        }
    }

    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public Result updateUserInfo(@RequestBody UserInfoUpdateRequest userInfoUpdateRequest) {
        try {
            boolean flag = userService.updateUserInfo(userInfoUpdateRequest);
            if(flag){
                return new Result(true, MessageConstant.EDIT_PERSON_SUCCESS);
            }else{
                return new Result(false, MessageConstant.EDIT_PERSON_FAIL);
            }
        } catch (Exception e) {
            return new Result(false, MessageConstant.EDIT_PERSON_FAIL);
        }
    }

    @RequestMapping(value = "/insertRole", method=RequestMethod.POST)
    public Result insertRoleForUser(@RequestBody InsertRoleForUserRequest insertRoleForUserRequest){
        try{
            boolean flag = userService.insertRoleForUser(insertRoleForUserRequest);
            if(flag){
                return new Result(true, MessageConstant.ADD_USER_ROLE_SUCCESS);
            }else{
                return new Result(false, MessageConstant.ADD_USER_ROLE_FAIL);
            }
        } catch (Exception e) {
            return new Result(false, MessageConstant.ADD_USER_ROLE_FAIL);
        }
    }

    @RequestMapping(value = "/getUserInfo",method = RequestMethod.GET)
    public Result getUserInfoByUserId(Integer id){
        try{
            UserResponse userInfo = userService.findUserById(id);
            return new Result(true,MessageConstant.QUERY_USER_SUCCESS,userInfo);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_USER_FAIL);
        }
    }

}

